package com.example.design.deprecated.state;

import com.example.design.utils.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//观察者模式具体被观察者和状态模式的状态具体类
@Component
public class DeprecatedCreateOrder extends DeprecatedAbstractOrderState{
    //将新生成的订单存放到redis中
    @Resource
    private RedisUtils redisUtils;

    //订单创建后下一个状态：待支付
    @Resource
    private DeprecatedPayOrder payOrder;

    //订单方法——创建订单
    @Override
    public DeprecatedOrder createOrder(String orderId, String productId, DeprecatedOrderContext context) {
        //创建订单对象，将状态设置为 ORDER_WAIT_PAY
        DeprecatedOrder order = DeprecatedOrder.builder()
                .orderId(orderId)
                .productId(productId)
                .state(ORDER_WAIT_PAY)
                .build();
        //将订单存入redis中，900s后过期
        redisUtils.set(orderId,order,900);
        //设置上下文角色的当前状态为待支付状态
        super.notifyObserver(orderId,ORDER_WAIT_PAY);
        return order;
    }
}
